package shop.up.item.service.impl;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import shop.up.item.service.ItemService;
import shop.up.list.client.ListFeignClient;
import shop.up.model.product.BaseCategoryView;
import shop.up.model.product.SkuInfo;
import shop.up.model.product.SpuSaleAttr;
import shop.up.product.client.ProductFeignClient;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 商品详情接口实现 {@link ItemServiceImpl}
 *
 * @author Kevin
 * @date 2022/7/6
 */
@Service
public class ItemServiceImpl implements ItemService {

    // 引用远程feign接口
    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;



    /**
     * 采用异步编排多任务进行优化
     * @param skuId
     * @return
     */
    @Override
    public Map<String, Object> getBySkuId(Long skuId) {
        Map<String, Object> result = new HashMap<>();

        // 通过skuId 查询skuInfo
        CompletableFuture<SkuInfo> skuCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            // 保存skuInfo
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        // 销售属性-销售属性值回显并锁定
        CompletableFuture<Void> spuSaleAttrCompletableFuture = skuCompletableFuture.thenAcceptAsync(skuInfo -> {
            // 销售属性-销售属性值回显并锁定
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient
                    .getSpuSaleAttrListCheckBySku(skuInfo.getId(), skuInfo.getSpuId());
            // 保存数据
            result.put("spuSaleAttrList",spuSaleAttrList);
        });

        //根据spuId查询map集合属性
        CompletableFuture<Void> skuValueIdsMapCompletableFuture = skuCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            // 保存 json字符串
            String valuesSkuJson = JSON.toJSONString(skuValueIdsMap);
            // 保存valuesSkuJson
            result.put("valuesSkuJson", valuesSkuJson);
        });

        //获取商品分类
        CompletableFuture<Void> categoryViewCompletableFuture = skuCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            //保存商品分类数据
            result.put("categoryView", categoryView);
        });

        // 获取价格
        CompletableFuture<Void> skuPriceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            result.put("price", skuPrice);
        }, threadPoolExecutor);

        // 更新sku热点
        CompletableFuture<Void> incrHotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);

        // 多任务组合
        CompletableFuture.allOf(
                skuCompletableFuture,
                spuSaleAttrCompletableFuture,
                skuValueIdsMapCompletableFuture,
                skuPriceCompletableFuture,
                categoryViewCompletableFuture,
                incrHotScoreCompletableFuture)
                .join();

        return result;
    }

    /**
     * 获取sku详情
     * @param skuId
     * @return
     */
    // @Override
    // public Map<String, Object> getBySkuId(Long skuId) {
    //     Map<String, Object> result = new HashMap<>();
    //
    //     // 通过skuId 查询skuInfo
    //     SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
    //     // 销售属性-销售属性值回显并锁定
    //     List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuInfo.getId(), skuInfo.getSpuId());
    //
    //     //根据spuId查询map集合属性
    //     Map skuValueIdsMap =  productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
    //
    //     //获取商品最新价格
    //     BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
    //     //获取商品分类
    //     BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
    //
    //     //保存商品分类数据
    //     result.put("categoryView",categoryView);
    //     // 保存 json字符串
    //     String valuesSkuJson = JSON.toJSONString(skuValueIdsMap);
    //     // 获取价格
    //     result.put("price",skuPrice);
    //     // 保存valuesSkuJson
    //     result.put("valuesSkuJson",valuesSkuJson);
    //     // 保存数据
    //     result.put("spuSaleAttrList",spuSaleAttrList);
    //     // 保存skuInfo
    //     result.put("skuInfo",skuInfo);
    //
    //     return result;
    // }
}
